<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2005/08/07 05:20:29 $)
// $RCSfile: upgrade_step2.php,v $ : $Revision: 1.5 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	// File being called directly, end it
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($dbCore->connected)
{
	$userData = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."staff` WHERE `username` = '". addslashes($_POST["username"]) ."' AND `password` = '". addslashes(md5($_POST["password"])) ."';");

	if ($userData["username"] == $_POST["username"] && trim($userData["username"]) != "")
	{
		// Authenticated, Everything is fine
			
		// Get the current version and other settings like registered modules, we only upgrade those modules that are registered
		$Record = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."settings` WHERE `section` = 'core' AND `vkey` = 'version';");
		$currentversion = $Record["data"];

		// Now get the registered modules
		$registeredmodules = array();
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."settings` WHERE `section` = 'registeredmodules';");
		while ($dbCore->nextRecord())
		{
			$registeredmodules[] = $dbCore->Record["vkey"];
		}
		// Add core module, its not registered anywhere
		$registeredmodules[] = "core";

		if (!is_array($registeredmodules))
		{
			$_SWIFT["error"] = true;
			$_SWIFT["errormsg"] = "Invalid Settings Table, Upgrade Wizard was unable to recover from this error, Please contact support";
		} else {
			// Ok everything is fine now, we have both the current version and the registered modules
			// Get the next version from version table, its under /swift/setup/upgrade
			$nextversion = getNextVersion($currentversion);
			if (!$nextversion)
			{
				// We are up to date, time to finish the wizard
				$_POST["step"] = "3";
			} else if (version_compare($currentversion, $nextversion) == -1) {
				// super, we are ready to go, vrrooom vrrooooooooomm.. go!

				$_SWIFT["versiontable"] = getUpgradeVersionTable($currentversion);
					
				// Now include the file that will do all the magic
				$nextversionfile = dirname(__FILE__)."/../upgrade/".$nextversion."/upgrade.php";
				if (!file_exists($nextversionfile))
				{
					// This is a serious matter, we have a dead version here, call 911!
					trigger_error("SWIFT Error: This is a critical error, $nextversionfile file does not exist, Please contact support", E_USER_ERROR);
				}

				if (empty($_POST["page"]))
				{
					$_POST["page"] = "1";
				}

				displayHeader();
				// Ok mate require it
				require_once ($nextversionfile);

				// Get total number of pages that we need to go through
				$totalpages = $_UPGRADE["pages"];
				if (empty($totalpages))
				{
					$totalpages = 1;
				}

				// Display Current Page etc
				displayMessage("Version: $nextversion (Page ". $_POST["page"] ." of $totalpages)");
		
				// Execute the queries
				execAllQueries();

				// Calculate next step page
				if ($_POST["page"] >= $totalpages)
				{
					// We have crossed the page or equalled our page, The upgrade file should have updated the version now, we do nothing except for pass on an empty page, if by any chance there is another version to be upgraded to the upgrade wizard will find it on next execution and iterate through its pages
					$_SWIFT["execpage"] = "0";
				} else {
					// We are not more than it yet, increment it
					$_SWIFT["execpage"] = $_POST["page"]+1;
				}

				displayFooter();

			} else {
				// This should not happen under any case, unless some user tried to act smart and created a directory under upgrade.. god save such people!
				$_SWIFT["error"] = true;
				$_SWIFT["errormsg"] = "Core Version Mismatch, Upgrade Wizard cannot recover from this error, Current Version is: $currentversion and Next Version is: $nextversion";
			}
		}
	} else {
		$_POST["step"] = "1";
		$_SWIFT["errormsg"] = true;
		$_SWIFT["errormsg"] = "Invalid Username or Password";
	}
} else {
	$_SWIFT["error"] = true;
}
?>